var $body = $('body'),
    $window = $(window),
    $overlay = $('.full-overlay');

// DROPDOWN
$('.drop-open').on('click', function(){
    var $content = $(this).siblings('.drop-content');
    if($(window).width() < 640){
        $content.slideToggle(300);
    }
});

// OFF-CANVAS
var scrollTopMenu;
function toggle_menu(){
    if(!$body.hasClass('menu-on')){
        scrollTopMenu = $window.scrollTop();
        $('main').css('margin-top', -scrollTopMenu);
        $('html').css('overflow', 'hidden');
        $body.addClass('menu-on');
        $overlay.fadeIn();
    } else {
        $('main').attr('style', '');
        $('html').attr('style', '');
        $body.removeClass('menu-on');
        $window.scrollTop(scrollTopMenu);
        $overlay.fadeOut();
    }
}

$('#toggle-menu').on('click', function(){
    toggle_menu();
});

$overlay.on('click', function(){
    toggle_menu();
});

// BACK TO TOP
$('.footer-to-top').on('click', function(event){
    event.preventDefault();
    $window.scrollTop(0);
});

$(document).on('click', '[data-modal="share"]', function(event) {
    event.preventDefault();
    var $a = $(this);
    popupCenter($a.attr('href'), 'Compartir'/*+$a.data('title')*/, 580, 470);
});

var popupCenter = function(url, title, w, h) {
    // Fixes dual-screen position                         Most browsers      Firefox
    var dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : screen.left;
    var dualScreenTop = window.screenTop !== undefined ? window.screenTop : screen.top;

    var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
    var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;

    var left = ((width / 2) - (w / 2)) + dualScreenLeft;
    var top = ((height / 3) - (h / 3)) + dualScreenTop;

    var newWindow = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);

    // Puts focus on the newWindow
    if (window.focus) {
        newWindow.focus();
    }
};

var last_tweet_container, last_tweet;
    
if (last_tweet_container = document.getElementById('last-tweet')) {
    last_tweet = last_tweet_container.innerHTML;
    last_tweet = last_tweet.split(' ');
    for(i = 0; i < last_tweet.length; i++){
        if(last_tweet[i].indexOf('http') > -1){
            last_tweet[i] = '<a href="'+last_tweet[i]+'" target="_blank">'+last_tweet[i]+'</a>';
        }
    }
    last_tweet = last_tweet.join(' ');
    last_tweet_container.innerHTML = last_tweet;
} 



//buscar
$('.top-bar-search button').click(function (event) {
    event.preventDefault();
    var input = $(this).prev().val().toLowerCase().replace(/[^0-9a-zá-úñ_ ]+/g, '').replace(/\s/g, '+');
    if (input) window.location = 'busqueda/'+input
})
$('.top-bar-search input').bind('keypress', function(e) {
    if(e.keyCode==13){
        event.preventDefault();
        var input = $(this).val().toLowerCase().replace(/[^0-9a-zá-úñ_ ]+/g, '').replace(/\s/g, '+');
        if (input) window.location = 'busqueda/'+input
    }
});